home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-13  |  5.5 KB  |  294 lines

  1. /* Miscellaneous machine independent utilities
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "socket.h"
  7. #include "mbuf.h"
  8.  
  9. char Whitespace[] = " \t\r\n";
  10.  
  11. /* convert a tcp port number or name to integer - WG7J */
  12. int
  13. atoip(s)
  14. char *s;
  15. {
  16.     int p,n;
  17.  
  18.     if((p=atoi(s)) == 0) {
  19.         n = strlen(s);
  20.         if(!strncmp(s,"convers",n))
  21.             p = IPPORT_CONVERS;
  22.         else if(!strncmp(s,"telnet",n))
  23.             p = IPPORT_TELNET;
  24.         else if(!strncmp(s,"ttylink",n))
  25.             p = IPPORT_TTYLINK;
  26.     }
  27.     return p;
  28. }
  29.  
  30. /* Select from an array of strings, or return ascii number if out of range */
  31. char *
  32. smsg(msgs,nmsgs,n)
  33. char *msgs[];
  34. unsigned nmsgs,n;
  35. {
  36.     static char buf[16];
  37.  
  38.     if(n < nmsgs && msgs[n] != NULLCHAR)
  39.         return msgs[n];
  40.     sprintf(buf,"%u",n);
  41.     return buf;
  42. }
  43.  
  44. /* Convert hex-ascii to integer */
  45. int
  46. htoi(s)
  47. char *s;
  48. {
  49.     int i = 0;
  50.     char c;
  51.  
  52.     while((c = *s++) != '\0'){
  53.         if(c == 'x')
  54.             continue;    /* allow 0x notation */
  55.         if('0' <= c && c <= '9')
  56.             i = (i * 16) + (c - '0');
  57.         else if('a' <= c && c <= 'f')
  58.             i = (i * 16) + (c - 'a' + 10);
  59.         else if('A' <= c && c <= 'F')
  60.             i = (i * 16) + (c - 'A' + 10);
  61.         else
  62.             break;
  63.     }
  64.     return i;
  65. }
  66. /* Convert single hex-ascii character to binary */
  67. int
  68. htob(c)
  69. char c;
  70. {
  71.     if('0' <= c && c <= '9')
  72.         return c - '0';
  73.     else if('a' <= c && c <= 'f')
  74.         return c - 'a' + 10;
  75.     else if('A' <= c && c <= 'F')
  76.         return c - 'A' + 10;
  77.     else
  78.         return -1;
  79. }
  80. /* Read an ascii-encoded hex string, convert to binary and store in
  81.  * output buffer. Return number of bytes converted
  82.  */
  83. int
  84. readhex(out,in,size)
  85. char *out,*in;
  86. int size;
  87. {
  88.     int c,count;
  89.  
  90.     if(in == NULLCHAR)
  91.         return 0;
  92.     for(count=0;count < size;count++){
  93.         while(*in == ' ' || *in == '\t')
  94.             in++;    /* Skip white space */
  95.         if((c = htob(*in++)) == -1)
  96.             break;    /* Hit non-hex character */
  97.         out[count] = c << 4;    /* First nybble */
  98.         while(*in == ' ' || *in == '\t')
  99.             in++;    /* Skip white space */
  100.         if((c = htob(*in++)) == -1)
  101.             break;    /* Hit non-hex character */
  102.         out[count] |= c;    /* Second nybble */
  103.     }
  104.     return count;
  105. }
  106.  
  107. /* replace terminating end of line marker(s) with null */
  108. void
  109. rip(s)
  110. register char *s;
  111. {
  112.     register char *cp;
  113.  
  114.     if((cp = strchr(s,'\n')) != NULLCHAR)
  115.         *cp = '\0';
  116. }
  117.  
  118. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  119.  * library, but it doesn't call mallocw() and can therefore return NULL.
  120.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  121.  */
  122. char *
  123. strdup(s)
  124. const char *s;
  125. {
  126.     register char *out;
  127.     register int len;
  128.  
  129.     if(s == NULLCHAR)
  130.         return NULLCHAR;
  131.     len = strlen(s);
  132.     out = mallocw(len+1);
  133.     /* This is probably a tad faster than strcpy, since we know the len */
  134.     memcpy(out,s,len);
  135.     out[len] = '\0';
  136.     return out;
  137. }
  138. /* Routines not needed for Turbo 2.0, but available for older libraries */
  139. #ifdef    AZTEC
  140.  
  141. /* Case-insensitive string comparison */
  142. strnicmp(a,b,n)
  143. register char *a,*b;
  144. register int n;
  145. {
  146.     char a1,b1;
  147.  
  148.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  149.         if(a1 == b1)
  150.             continue;    /* No need to convert */
  151.         a1 = tolower(a1);
  152.         b1 = tolower(b1);
  153.         if(a1 == b1)
  154.             continue;    /* NOW they match! */
  155.         if(a1 > b1)
  156.             return 1;
  157.         if(a1 < b1)
  158.             return -1;
  159.     }
  160.     return 0;
  161. }
  162.  
  163. char *
  164. strtok(s1,s2)
  165. char *s1;    /* Source string (first call) or NULL */
  166. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  167. const char *s2;    /* Delimiter string */
  168. #else
  169. char *s2;    /* Delimiter string */
  170. #endif
  171. {
  172.     static int isdelim();
  173.     static char *next;
  174.     register char *cp;
  175.     char *tmp;
  176.  
  177.     if(s2 == NULLCHAR)
  178.         return NULLCHAR;    /* Must give delimiter string */
  179.  
  180.     if(s1 != NULLCHAR)
  181.         next = s1;        /* First call */
  182.  
  183.     if(next == NULLCHAR)
  184.         return NULLCHAR;    /* No more */
  185.  
  186.     /* Find beginning of this token */
  187.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  188.         ;
  189.  
  190.     if(*cp == '\0')
  191.         return NULLCHAR;    /* Trailing delimiters, no token */
  192.  
  193.     /* Save the beginning of this token, and find its end */
  194.     tmp = cp;
  195.     next = NULLCHAR;    /* In case we don't find another delim */
  196.     for(;*cp != '\0';cp++){
  197.         if(isdelim(*cp,s2)){
  198.             *cp = '\0';
  199.             next = cp + 1;    /* Next call will begin here */
  200.             break;
  201.         }
  202.     }
  203.     return tmp;
  204. }
  205. static int
  206. isdelim(c,delim)
  207. char c;
  208. register char *delim;
  209. {
  210.     char d;
  211.  
  212.     while((d = *delim++) != '\0'){
  213.         if(c == d)
  214.             return 1;
  215.     }
  216.     return 0;
  217. }
  218. #endif    /* AZTEC */
  219.  
  220.  
  221.  
  222. /* Host-network conversion routines, replaced on the x86 with
  223.  * assembler code in pcgen.asm
  224.  */
  225. #ifndef    MSDOS
  226. /* Put a long in host order into a char array in network order */
  227. char *
  228. put32(cp,x)
  229. register char *cp;
  230. int32 x;
  231. {
  232.     *cp++ = x >> 24;
  233.     *cp++ = x >> 16;
  234.     *cp++ = x >> 8;
  235.     *cp++ = x;
  236.     return cp;
  237. }
  238. /* Put a short in host order into a char array in network order */
  239. char *
  240. put16(cp,x)
  241. register char *cp;
  242. int16 x;
  243. {
  244.     *cp++ = x >> 8;
  245.     *cp++ = x;
  246.  
  247.     return cp;
  248. }
  249. int16
  250. get16(cp)
  251. register char *cp;
  252. {
  253.     register int16 x;
  254.  
  255.     x = uchar(*cp++);
  256.     x <<= 8;
  257.     x |= uchar(*cp);
  258.     return x;
  259. }
  260. /* Machine-independent, alignment insensitive network-to-host long conversion */
  261. int32
  262. get32(cp)
  263. register char *cp;
  264. {
  265.     int32 rval;
  266.  
  267.     rval = uchar(*cp++);
  268.     rval <<= 8;
  269.     rval |= uchar(*cp++);
  270.     rval <<= 8;
  271.     rval |= uchar(*cp++);
  272.     rval <<= 8;
  273.     rval |= uchar(*cp);
  274.  
  275.     return rval;
  276. }
  277. /* Compute int(log2(x)) */
  278. int
  279. log2(x)
  280. register int16 x;
  281. {
  282.     register int n = 16;
  283.     for(;n != 0;n--){
  284.         if(x & 0x8000)
  285.             break;
  286.         x <<= 1;
  287.     }
  288.     n--;
  289.     return n;
  290. }
  291.  
  292. #endif
  293.  
  294.